<?php
/**
 * Kelas user
 */
class agenda extends CI_Controller {
	
	 //Konstruktor
	 
	function __construct()
	{
		parent::__construct();
		$this->load->model('agenda_model');	
		$this->load->library('form_validation');
		$this->load->helper(array('form','url'));
	  $this->cek_session();
	
		//$this->cek_session();
	}
	
	function cek_session()
	 {	
		$kode_role = $this->session->userdata('id_jabatan');
		if($kode_role == NULL)
		{
			//redirect('login/login_ulang');
			redirect('login');
		}//else if($kode_role != 1){
			//redirect('login/alert');
		//}
	 }
	
	
	/**
	 * Menampilkan tabel daftar user
	 */
	 function index()
	 {
			$this->load->model('agenda_tim_model');
			$data_konten['agenda']=$this->agenda_model->get_all_agenda($this->session->userdata('id_organisasi'));
			$agenda2=$this->agenda_model->get_all_agenda($this->session->userdata('id_organisasi'));
			$anggota_tim = '';
			foreach($agenda2->result() as $row){
				$data_konten['agenda2']['nama_agenda'] = $row->nama_agenda;
				$data_konten['agenda2']['tanggal_mulai'] = date('d F Y', strtotime($row->tanggal_mulai)).' ('.$row->jam_mulai.')';
				$data_konten['agenda2']['tanggal_selesai'] = date('d F Y', strtotime($row->tanggal_selesai)).' ('.$row->jam_selesai.')';				
				$data_konten['agenda2']['jenis_agenda'] = $row->jenis_agenda;				
				$data_konten['agenda2']['status_agenda'] = $row->status_agenda;								
				$data_konten['agenda2']['id_agenda'] = $row->id_agenda;							
				$data_konten['agenda2']['id_ketua'] = $row->id_ketua;				
				if($row->link_hasil_liputan == ''){ $data_konten['agenda2']['link_hasil_liputan'] = '-'; } 
				else { $data_konten['agenda2']['link_hasil_liputan'] = $row->link_hasil_liputan.'(Tanggal Upload : '.date('d F Y', strtotime($row->tanggal_upload)).')'; }				
				$tim = $this->agenda_tim_model->ambil_daftar_peminjaman($row->id_agenda);	
				foreach ($tim->result() as $row){
					$anggota_tim = $anggota_tim.', '.$row->nama_anggota;
				}
				$data_konten['agenda2']['anggota_tim'] = $anggota_tim;
			}
			$data["menu"] = 'agenda';
			$data["sub_menu"] = 'daftar_agenda';
		$data["konten"] = $this->load->view("daftar_agenda", $data_konten, TRUE);
			$this->load->view("main", $data);
	 }
	 
	 function index2()
	 {
			
			$this->load->model('agenda_tim_model');
			$data_konten['agenda']=$this->agenda_model->get_all_agenda($this->session->userdata('id_organisasi'));
			$agenda2=$this->agenda_model->get_all_agenda($this->session->userdata('id_organisasi'));
			$anggota_tim = '';
			foreach($agenda2->result() as $row){
				$data_konten['agenda2']['nama_agenda'] = $row->nama_agenda;
				$data_konten['agenda2']['tanggal_mulai'] = date('d F Y', strtotime($row->tanggal_mulai)).' ('.$row->jam_mulai.')';
				$data_konten['agenda2']['tanggal_selesai'] = date('d F Y', strtotime($row->tanggal_selesai)).' ('.$row->jam_selesai.')';				
				$data_konten['agenda2']['jenis_agenda'] = $row->jenis_agenda;				
				$data_konten['agenda2']['status_agenda'] = $row->status_agenda;								
				$data_konten['agenda2']['id_agenda'] = $row->id_agenda;							
				$data_konten['agenda2']['id_ketua'] = $row->id_ketua;				
				if($row->link_hasil_liputan == ''){ $data_konten['agenda2']['link_hasil_liputan'] = '-'; } 
				else { $data_konten['agenda2']['link_hasil_liputan'] = $row->link_hasil_liputan.'(Tanggal Upload : '.date('d F Y', strtotime($row->tanggal_upload)).')'; }				
				$tim = $this->agenda_tim_model->ambil_daftar_peminjaman($row->id_agenda);	
				foreach ($tim->result() as $row){
					$anggota_tim = $anggota_tim.', '.$row->nama_anggota;
				}
				$data_konten['agenda2']['anggota_tim'] = $anggota_tim;
			}
			$data["menu"] = 'agenda';
			$data["sub_menu"] = 'daftar_agenda';
		$data["konten"] = $this->load->view("daftar_agenda2", $data_konten, TRUE);
			$this->load->view("main", $data);
	 }
	 
	 function all()
	 {
			$data_konten['agenda']=$this->agenda_model->get_all_in_agenda($this->session->userdata('id_organisasi'));
      $data["menu"] = 'agenda';
			$data["sub_menu"] = 'daftar_agenda_all';
		  $data["konten"] = $this->load->view("daftar_agenda_all", $data_konten, TRUE);
			$this->load->view("main", $data);
	 }
	 
	 
	 function detail_agenda($id_agenda)
	 {
			
      $data_konten['agenda']=$this->agenda_model->ambil_detail_agenda($id_agenda);
	  $data_konten['id_org']=$this->agenda_model->ambil_detail_agenda($id_agenda)->row()->id_organisasi;
			
			//$halaman['konten'] = $this->load->view('daftar_info_karir',$data_konten,TRUE);
			$data["konten"] = $this->load->view("detail_agenda", $data_konten, TRUE);
			$data["menu"] = 'agenda';
		if($this->session->userdata('id_organisasi') == $this->agenda_model->ambil_detail_agenda($id_agenda)->row()->id_organisasi){
			$data["sub_menu"] = 'daftar_agenda';
			} else{
			$data["sub_menu"] = 'daftar_agenda_all';
			}
      $this->load->view("main", $data);
	 }
	 
	 function edit_agenda($id_agenda)
	 {
		  $agenda=$this->agenda_model->ambil_detail_agenda($id_agenda);	
			$data_konten['id_agenda'] = $agenda->row()->id_agenda;
      $data_konten['nama_agenda'] = $agenda->row()->nama_agenda;
      $data_konten['lokasi_agenda'] = $agenda->row()->lokasi_agenda;
      $tanggal_mulai = explode('-', $agenda->row()->tanggal_mulai);
			$tanggal_mulai2 = $tanggal_mulai[1].'/'.$tanggal_mulai[2].'/'.$tanggal_mulai[0];
			$data_konten['tanggal_mulai'] = $tanggal_mulai2;
      $tanggal_selesai = explode('-', $agenda->row()->tanggal_selesai);
			$tanggal_selesai2 = $tanggal_selesai[1].'/'.$tanggal_selesai[2].'/'.$tanggal_selesai[0];
			$data_konten['tanggal_selesai'] = $tanggal_selesai2;
			$jam_mulai = explode(':', $agenda->row()->jam_mulai);
			$data_konten['jam_mulai'] = $jam_mulai[0];
      $jam_selesai = explode(':', $agenda->row()->jam_selesai);
			$data_konten['jam_selesai'] = $jam_selesai[0];
			$data_konten['tanggal_upload'] = $agenda->row()->tanggal_upload;
			$data_konten['status_agenda'] = $agenda->row()->status_agenda;
      $data_konten['jenis_agenda'] = $agenda->row()->jenis_agenda;
      $data_konten['link_hasil_liputan'] = $agenda->row()->link_hasil_liputan;
      $data_konten['catatan_tambahan'] = $agenda->row()->catatan_tambahan;
      $data_konten['id_organisasi'] = $agenda->row()->id_organisasi;
     
			$data["konten"] = $this->load->view("edit_agenda", $data_konten, TRUE);
			$data["menu"] = 'agenda';
			$data["sub_menu"] = 'daftar_agenda';
      $this->load->view("main", $data);
	 }
	 
	 function delete_agenda($id_agenda)
	 {
			
			$data_konten['agenda']=$this->agenda_model->hapus_agenda($id_agenda);
			$this->agenda_model->update_agendaku(NULL,$this->agenda_model->ambil_detail_agenda_byref($id_agenda)->row()->id_agenda);
			redirect('agenda');
	 }
   function updateAgenda($id_agenda)	{
		
    if($this->cek_validasi() == true)
		{
		  $tanggal_mulai = explode('/', $this->input->post('tanggal_mulai'));
			$tanggal_mulai2 = $tanggal_mulai[2].'-'.$tanggal_mulai[0].'-'.$tanggal_mulai[1];
			$tanggal_selesai = explode('/',$this->input->post('tanggal_selesai'));
			$tanggal_selesai2 = $tanggal_selesai[2].'-'.$tanggal_selesai[0].'-'.$tanggal_selesai[1];
			$jam_mulai = $this->input->post('jam_mulai').':'.$this->input->post('menit_mulai').':00';
			$jam_selesai = $this->input->post('jam_selesai').':'.$this->input->post('menit_selesai').':00';
			
			//jika finish maka tanggal upload di update sesuai tanggal sekarang, informasi blm tampil
			if($this->input->post('statusAgenda') == 'Finish'){
				$today = $this->input->post('tanggalUpload');
			} else {
				$today = date("Y").'-'.date("n").'-'.date("j");
			}
    
    $data["menu"] = 'agenda';
			$data["sub_menu"] = 'tambah_agenda';
    $agenda_baru = array('id_agenda' 		=> $this->input->post('id_agenda'),
            'nama_agenda' 		=> $this->input->post('namaAgenda'),
						'lokasi_agenda' 		=> $this->input->post('lokasi'),
						'tanggal_mulai'  		=> $tanggal_mulai2,
						'tanggal_selesai'	=> $tanggal_selesai2,
						'jam_mulai'  		=> $jam_mulai,
						'jam_selesai'	=> $jam_selesai,
						'tanggal_upload' 		=> $today,
						'jenis_agenda'  		=> $this->input->post('jenisAgenda'),
						'status_agenda'	=> $this->input->post('statusAgenda'),
						'link_hasil_liputan' 		=> $this->input->post('link'),
						'catatan_tambahan'  		=> $this->input->post('catatan'),
						'id_organisasi'		=> $this->session->userdata('id_organisasi')
						);

			$id_agenda_lama = $this->input->post('id_agenda');
			$this->agenda_model->update_agenda($agenda_baru, $id_agenda_lama);
			redirect('agenda');
			}
		else{
	redirect('agenda/edit_agenda/'.$this->input->post('id_agenda'));	
	}
	}
		
	 
	 function viewTambahAgenda()
	 {
			
			//$halaman['konten'] = $this->load->view('daftar_info_karir',$data_konten,TRUE);
			$data["konten"] = $this->load->view("tambah_agenda",'', TRUE);
      $data["menu"] = 'agenda';
			$data["sub_menu"] = 'tambah_agenda';
      $this->load->view('main',$data);
	 }
	
	function viewTambahAgenda2()
	 {
			
			//$halaman['konten'] = $this->load->view('daftar_info_karir',$data_konten,TRUE);
			$data["konten"] = $this->load->view("tambah_agendae",'', TRUE);
      $data["menu"] = 'agenda';
			$data["sub_menu"] = 'tambah_agenda';
      $this->load->view('main',$data);
	 }
   function tambahAgenda()
	{	
    if($this->cek_validasi() == true)
		{
      $tanggal_mulai = explode('/', $this->input->post('tanggal_mulai'));
			$tanggal_mulai2 = $tanggal_mulai[2].'-'.$tanggal_mulai[0].'-'.$tanggal_mulai[1];
			$tanggal_selesai = explode('/',$this->input->post('tanggal_selesai'));
			$tanggal_selesai2 = $tanggal_selesai[2].'-'.$tanggal_selesai[0].'-'.$tanggal_selesai[1];
			$jam_mulai = $this->input->post('jam_mulai').':'.$this->input->post('menit_mulai').':00';
			$jam_selesai = $this->input->post('jam_selesai').':'.$this->input->post('menit_selesai').':00';
		  $today = date('Y-m-d');

    $data["menu"] = 'agenda';
			$data["sub_menu"] = 'tambah_agenda';
    $agenda = array('nama_agenda' 		=> $this->input->post('namaAgenda'),
						'lokasi_agenda' 		=> $this->input->post('lokasi'),
						'tanggal_mulai'  		=> $tanggal_mulai2,
						'tanggal_selesai'	=> $tanggal_selesai2,
						'jam_mulai'  		=> $jam_mulai,
						'jam_selesai'	=> $jam_selesai,
            'tanggal_upload' 		=> $today,
						'jenis_agenda'  		=> $this->input->post('jenisAgenda'),
						'status_agenda'	=> $this->input->post('statusAgenda'),
						'link_hasil_liputan' 		=> $this->input->post('link'),
						'catatan_tambahan'  		=> $this->input->post('catatan'),
						'id_organisasi'		=> $this->session->userdata('id_organisasi')
						);
		
		
			$this->agenda_model->tambahAgenda($agenda);
			redirect('agenda/index2');
		}
		else{
	redirect('agenda/viewTambahAgenda2');	
	}
	}
	
	function tambahAgendaku($id_agenda)
	{	
    
    $data_konten=$this->agenda_model->get_all_in_agendaku($id_agenda,$this->session->userdata('id_organisasi'));
    $today = date('Y-m-d');
    $data["menu"] = 'agenda';
	  $data["sub_menu"] = 'tambah_agenda';
    
    $agenda = array('nama_agenda' 		=> $data_konten->row()->nama_agenda,
						'lokasi_agenda' 		=> $data_konten->row()->lokasi_agenda,
						'tanggal_mulai'  		=> $data_konten->row()->tanggal_mulai,
						'tanggal_selesai'	=> $data_konten->row()->tanggal_selesai,
						'jam_mulai'  		=> $data_konten->row()->jam_mulai,
						'jam_selesai'	=> $data_konten->row()->jam_selesai,
            'tanggal_upload' 		=> $today,
						'jenis_agenda'  		=> $data_konten->row()->jenis_agenda,
						'status_agenda'	=> $data_konten->row()->status_agenda,
						'link_hasil_liputan' 		=> $data_konten->row()->link_hasil_liputan,
						'catatan_tambahan'  		=> $data_konten->row()->catatan_tambahan,
						'id_referensi'  		=> $data_konten->row()->id_agenda,
            'id_organisasi'		=> $this->session->userdata('id_organisasi')
						);
		//if($this->cek_validasi() == true)
		//{
		$this->agenda_model->tambahAgenda($agenda);
		$id_agenda_lama = $data_konten->row()->id_agenda;
		$id_agendaku = $this->agenda_model->ambil_detail_agenda_byref($id_agenda_lama)->row()->id_agenda;
		
	    $this->agenda_model->update_agendaku($id_agendaku,$id_agenda_lama);		
		
		redirect('agenda/detail_agenda/'.$id_agendaku);
		//}
		//else{
			//$this->load->view('tambah_agenda');
	//	}
	}

function cek_validasi()
	{	
		if($this->input->post('statusAgenda') == 'Finish'){
			$this->form_validation->set_rules('link', 'hasil liputan', 'required');			
		} 
		
		$this->form_validation->set_rules('namaAgenda', 'nama agenda', 'required');
		$this->form_validation->set_rules('lokasi', 'lokasi', 'required');
		$this->form_validation->set_rules('tanggal_mulai', 'keterangan', 'required');
		$this->form_validation->set_rules('tanggal_selesai', 'kondisi barang', 'required');
		$this->form_validation->set_rules('jam_mulai', 'jam mulai', 'required');
		$this->form_validation->set_rules('jam_selesai', 'jam selesai', 'required');
    $this->form_validation->set_rules('menit_mulai', 'jam mulai', 'required');
		$this->form_validation->set_rules('menit_selesai', 'jam selesai', 'required');
    $this->form_validation->set_rules('jenisAgenda', 'jenis agenda', 'required');
	  $this->form_validation->set_rules('statusAgenda', 'status agenda', 'required');
    $this->form_validation->set_message('required', '<label for="error" generated="true" class="error" style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Kolom %s harus diisi !!</label>');
		return $this->form_validation->run();
	}	 
	
	
	function cek_dropdown($value){
		if($value == 0){
			$this->form_validation->set_message('cek_dropdown', 'Kolom %s harus dipilih!!');
			return FALSE;
		}
		else{
			return TRUE;
		}
	}
	
	function cek_password_ulang($value){
		if($value != $this->input->post('password')){
			$this->form_validation->set_message('cek_password_ulang', 'Kolom %s harus sama!!');
			return FALSE;
		}
		else{
			return TRUE;
		}
	}
	
	function cek_username($value){
		if($this->user_model->cek_username($value)){
			$this->form_validation->set_message('cek_username', 'Username telah terpakai!!');
			return FALSE;
		}
		else{
			return TRUE;
		}
	}
	
	function cek_username_baru($value, $kode_user){
		if($this->user_model->cek_username_baru($value, $kode_user)){
			$this->form_validation->set_message('cek_username_baru', 'Username '.$value.' telah terpakai!!');
			return FALSE;
		}
		else{
			return TRUE;
		}
	}

	function cek_nip($value){
		if($this->user_model->cek_nip($value)){
			$this->form_validation->set_message('cek_nip', 'NIP telah terpakai!!');
			return FALSE;
		}
		else{
			return TRUE;
		}
	}
	
	function cek_nip_baru($value, $kode_user){
		if($this->user_model->cek_nip_baru($value, $kode_user)){
			$this->form_validation->set_message('cek_nip_baru', 'NIP '.$value.' telah terpakai!!');
			return FALSE;
		}
		else{
			return TRUE;
		}
	}
	
	function cek_pti($value){
		if($this->user_model->cek_pti($value)){
			$this->form_validation->set_message('cek_pti', 'Nama Perguruan Tinggi telah terpakai!!');
			return FALSE;
		}
		else{
			return TRUE;
		}
	}

	function cek_pti_baru($value, $kode_user){
		$tes = $this->user_model->get_nama_perguruan_tinggi($value);
		if($this->user_model->cek_pti_baru($value, $kode_user)){
			$this->form_validation->set_message('cek_pti_baru', 'Nama Perguruan Tinggi '.$tes->row()->NAMA_PTI.' telah terpakai!!');
			return FALSE;
		}
		else{
			return TRUE;
		}
	}
	
} // END user class

/* End of file user.php */
/* Location: ./system/application/controllers/manajemen_user.php */
